package model;

import Repository.Repository;
import Repository.RepositoryFactory;
import constants.SMSConstants;

public class Assigned implements SMSObjectState {

	private final String NAME = "Assigned";
	RuleSelector ruleSelector;
	NextStateCalculator stateCalculator;
	CalculateNextStateStrategy strategy;
	Repository repository;

	@Override
	public void execute(SMSObject smsObj) {
		if (smsObj instanceof Lead) {
			// System.out.println("Executing Assign State...");
			ruleSelector = new MergeRuleSelector(smsObj);
			Rule rule = ruleSelector.getRule();

			Repository leadRepository = RepositoryFactory.getRepository(SMSConstants.LEAD);
			stateCalculator = new NextStateCalculator();
			SMSObjectState newState = stateCalculator.getNextState(
					smsObj, ruleSelector);
			if(newState.getStateName().equals(NAME)){
				System.out.println("****Lead could not be merged as it does not comply to the rules****");
				return;
			}
			System.out.println("Duplicate Leads have been removed successfully.");
			smsObj.setCurrentState(newState);
			leadRepository.update(smsObj.getSmsObjId(), smsObj);
		}

	}

	public String getStateName() {
		return NAME;
	}

}
